#define CommInOut_Variable

#include "userDef.h"


parameter_out_typeDef parameter_out;
timer_CommInOut_typeDef timer_CommInOut;




void CommInOut_Tx(U8 *tx_buf, U8 tx_num) /* 零火线通讯发送 */
{
    CommInOut.tx_address = tx_buf;
    CommInOut.tx_count = tx_num;

    CommInOut_Tx_Intp();
}

void CommInOut_Tx_Intp(void) /* 零火线通讯发送中断 */
{
    if (CommInOut.tx_count > 0)
    {
        SCI1->TXD2 = *CommInOut.tx_address;
        CommInOut.tx_address++;
        CommInOut.tx_count--;
    }
    else
    {
        timer_CommInOut.txEndDelay = 50;
    }
}

void CommInOut_Rx(U8 *rx_buf, U8 rx_num) /* 零火线通讯接收 */
{
    CommInOut.status = RXDDATA;

    CommBuffClr();

    timer_CommInOut.commRun = 0;

    CommInOut.rx_count = 0U;
    CommInOut.rx_length = rx_num;
    CommInOut.rx_address = rx_buf;
}

void CommInOut_Rx_Intp(U8 data) /* 零火线通讯接收中断 */
{
    if (CommInOut.status == TXDDATA || CommInOut.status == TXDDEAL)
    {
        return;
    }

    if (CommInOut.rx_count == 0)
    {
        if (data != 0x01 && data != 0x02 && data != 0x0F && data != 0x11 && data != 0x12)
        {
            return;
        }
        else
        {
            timer_CommInOut.commRun = 0;
        }
    }

    if (CommInOut.rx_length > CommInOut.rx_count)
    {
        *CommInOut.rx_address = data;
        CommInOut.rx_address++;
        CommInOut.rx_count++;

        timer_CommInOut.tsfrmwt = 50; /* 50 * 0.5ms = 25ms */
    }
}

void CommInOut_Act(void) /* called in main.c */
{
    if (timer_CommInOut.commRun >= T_SCOMCYC) /* 通讯超时：1100 * 0.5ms = 550ms */
    {
        CommInOut_Rx((U8 *)buff_InOut, 20);
    }

    switch (CommInOut.status)
    {
    case RXDDATA: // 0
        if (timer_CommInOut.commRun >= T_SRXCYC || (timer_CommInOut.tsfrmwt == 0 && CommInOut.rx_count != 0))
        { /* 接收超时：500 * 0.5ms = 250ms，50 * 0.5ms = 25ms */
            CommInOut_Rx((U8 *)buff_InOut, 20);
            __NOP();
        }
        else if (CommInOut.rx_count >= 20)
        {
            if (RxdCheck() == 1)
            {
                CommInOut.status = RXDDEAL;

                timer_CommInOut.noComm = 0;

                f_NeedOutPower = 0;

                ReOutPowerSec = 0;
                ReOutPowerMin = 0;
                ReOutPowerHour = 0;
            }
            else
            {
                CommInOut_Rx((U8 *)buff_InOut, 20);
            }
        }
        break;

    case RXDDEAL: // 1
        RxdData();

        if (f_CommRstEnd == 0)
        {
            if (commOrder == 0x01)
            {
                f_CommRstEnd = 1;
                timer_CommInOut.commRstErr = T_SCHGERR;
            }
            else
            {
                f_TxdResetReq = 1;
            }
        }

        CommInOut.status = TXDDATA;
        break;

    case TXDDATA: // 2
        if (timer_CommInOut.commRun >= T_SRXCYC)
        {
            TxdData();

            CommInOut_Tx((U8 *)buff_InOut, 20);

            CommInOut.status = TXDDEAL;
        }
        break;

    case TXDDEAL: // 3
        if (CommInOut.tx_count == 0 && timer_CommInOut.txEndDelay == 0)
        {
            CommInOut_Rx((U8 *)buff_InOut, 20);
        }
        break;

    default:
        break;
    }
}

U8 CommGetBCC(void)
{
    U8 i, local_CheckNum = 0;

    for (i = 0; i < 19; i++)
    {
        local_CheckNum += buff_InOut[i].byte;
    }

    local_CheckNum = local_CheckNum ^ 0xAA;

    return local_CheckNum;
}

U8 CommGetBCC_SK(void)
{
    U8 i, local_CheckNum = 0;

    for (i = 0; i < 19; i++)
    {
        local_CheckNum += buff_InOut[i].byte;
    }

    local_CheckNum = local_CheckNum ^ 0x53;

    return local_CheckNum;
}

void CommBuffClr(void)
{
    U8 i;

    for (i = 0; i < 20; i++)
    {
        buff_InOut[i].byte = 0;
    }
}

U8 RxdCheck(void)
{
    if (CommGetBCC() == commCheck || CommGetBCC_SK() == commCheck)
    {
        if (CommGetBCC() == commCheck)
        {
            f_skflg = 0;
            // SCR00 = _8000_SAU_TRANSMISSION | _0000_SAU_INTSRE_MASK | _0200_SAU_PARITY_EVEN | _0080_SAU_LSB | _0010_SAU_STOP_1 |
            //         _0007_SAU_LENGTH_8;
        }
        else
        {
            f_skflg = 1;
            // SCR00 |= _8000_SAU_TRANSMISSION | _0000_SAU_INTSRE_MASK | _0300_SAU_PARITY_ODD | _0080_SAU_LSB | _0010_SAU_STOP_1 |
            //          _0007_SAU_LENGTH_8;
        }
        if (f_GetSlaveAddr == 0)
        {
            if ((commAddr & 0x0F) != slaveAddr)
            {
                slaveAddr = commAddr & 0x0F;
                count_GetAddr = 0;
            }
            else
            {
                count_GetAddr++;

                if (count_GetAddr >= 5)
                {
                    f_GetSlaveAddr = 1;
                }
            }

            return 1;
        }
        else
        {
            if ((commAddr & 0x0F) == slaveAddr)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
    else
    {
        return 0;
    }
}

void RxdData(void)
{
    f_OutCommErr = f_commFlag_b1;
    txdOrder = commOrder | 0x80;

    switch (commOrder)
    {
    case 0x01:
        f_RoomUnitOn = 0;
        f_TxdResetReq = 0;

        OutSafeErr1 = 0;
        OutSafeErr2 = 0;
        OutSafeErr3 = 0;
        OutSafeErr4 = 0;
        OutSafeErr5 = 0;
        OutSafeErr6 = 0;
        OutSafeErr7 = 0;
        OutSafeErr8 = 0;

        RxdOrder01();
        break;

    case 0x02:
        f_CommErr = 0;
        timer_CommInOut.commErr = 0;

        RxdOrder02();
        break;

    case 0x0F:
        break;

    case 0x11:
        f_CommErr = 0;
        timer_CommInOut.commErr = 0;

        RxdOrder11();
        break;

    case 0x12:
        f_CommErr = 0;
        timer_CommInOut.commErr = 0;

        RxdOrder12();
        break;
    }
}

void RxdOrder01(void)
{
    if (f_skflg == 1)
    {
        // D4
        parameter_out.indoorNum = (commData4 & 0x0F) + 1;

        // D7
        f_ExpExist = f_commData7_b0;

        if ((commData7 & 0x0E) != 0)
        {
            f_CompHeater = 1;
        }
        else
        {
            f_CompHeater = 0;
        }

        f_OutCSelf = f_commData7_b4;
    }
    else
    {
        // D0
        Protocol_VER = commData0;

        // D1
        parameter_out.indoorNum = ((commData1 & 0xF0) >> 4);

        // D2
        parameter_out.capacity = commData2;

        // D5
        parameter_out.comp_type = commData5;

        // D6
        parameter_out.ofan_type = commData6;

        /* D7 */
        if ((commData7 & 0x06) != 0)
        {
            f_CompHeater = 1;
        }
        else
        {
            f_CompHeater = 0;
        }

        f_OutCSelf = f_commData7_b5;
        f_ExpExist = f_commData7_b0;

        // D9
        Valve_Close = commData9;
    }
}

void RxdOrder02(void)
{
    if (f_skflg == 0)
    {
        OUT_BOARD_CODING_0 = commData0;
        OUT_BOARD_CODING_1 = commData1;
        OUT_BOARD_CODING_2 = commData2;
        OUT_BOARD_CODING_3 = commData3;
        OUT_BOARD_CODING_4 = commData4;

        OUT_PROGRAM_CODING_0 = commData5;
        OUT_PROGRAM_CODING_1 = commData6;
        OUT_PROGRAM_CODING_2 = commData7;
        OUT_PROGRAM_CODING_3 = commData8;

        OUT_PROGRAM_VER = commData9;

        EEPROM_CODING_0 = commData10;
        EEPROM_CODING_1 = commData11;
        EEPROM_CODING_2 = commData12;
        EEPROM_CODING_3 = commData13;

        EEPROM_VER = commData14;
    }
}

void RxdOrder11(void)
{
    U8 TempA;
    U8 local_outModeRxd;

    if (f_skflg == 1)
    {
        // D0
        f_CompOn = f_commData0_b4;
        f_ExpRest = f_commData0_b5;

        local_outModeRxd = parameter_out.run_mode;
        parameter_out.run_mode = (commData0 & 0x0F);

        if (parameter_out.run_mode == RXFORCEDEF)
        {
            set_manual_defrost(OFF);
        }

        if (local_outModeRxd == RXFORECALL)
        {
            if (parameter_out.run_mode != RXFORECALL)
            {
                if (f_FoRecall == 1)
                {
                    f_FoRecall = 0;
                    stat_FoRecall = 2;
                }
            }
        }
        else
        {
            if (parameter_out.run_mode == RXFORECALL)
            {
                if (f_FoRecall == 0)
                {
                    f_FoRecall = 1;
                    stat_FoRecall = 1;
                }
            }
        }

        if (local_outModeRxd == RXTESTRUN)
        {
            if (parameter_out.run_mode != RXTESTRUN)
            {
                TestRunStat = 0;
            }
        }
        else
        {
            if (parameter_out.run_mode == RXTESTRUN)
            {
                TestRunStat = 2;
            }
        }

        if (b_power.run == ON && e_mode.run == MODEHEAT)
        {
            if ((parameter_out.run_mode != RXHEAT) && (parameter_out.run_mode != RXFORCEHEAT) && (parameter_out.run_mode != RXFORCEDEF) && (parameter_out.run_mode != RXHEATTEST))
            {
                f_ModeClash = 1;
            }
            else
            {
                f_ModeClash = 0;
            }
        }

        // D1
        parameter_out.comp_freq = commData1;

        // D2
        parameter_out.ofan_speed = commData2;

        // D3

        // D4
        parameter_out.dc_current = commData4; // 接收到的压缩机相电流 * 10

        // D5 D6
        // if (parameter_out.indoorNum >= 2 && f_ExpExist == 1)
        //{
        parameter_out.eve_value = commData5 & 0x03;
        parameter_out.eve_value = parameter_out.eve_value << 8;
        parameter_out.eve_value += commData6;
        //}
        // else
        //{
        // parameter_out.eve_value = 200;
        //}

        // D7
        parameter_out.dc_voltage = commData7 * 4; // 接收到的直流母线电压

        // D8

        // D9
        temp.outdoor = commData9 * 10; // (实际室外环温值 + 40) * 10

        // D10
        temp.out_tube = commData10 * 10; // (实际室外铜管温值 + 40) * 10

        // D11
        temp.out_exha = commData11 * 10; // (实际室外排气温值 + 40) * 10

        // D12
        if (f_commData12_b1)
        {
            f_Defrosting = 1;
            set_manual_defrost(OFF);
        }
        else
        {
            f_Defrosting = 0;
        }

        f_4way = f_commData12_b7;
        f_HeaterOut = f_commData12_b0;
        // if (f_commData12_b3)//待机
        if (f_commData12_b2) // 回油
        {
            TempA = parameter_out.run_mode;

            if (TempA == RXHEAT || TempA == RXFORCEHEAT || TempA == RXHEATTEST || TempA == RXFORCEDEF)
            {
                f_Defrosting = 1;
                f_CoolOilBack = 0;
                f_HeatOilBack = 1;
            }
            else
            {
                f_CoolOilBack = 1;
                f_HeatOilBack = 0;
            }
        }
        else
        {
            f_CoolOilBack = 0;
            f_HeatOilBack = 0;
        }

        if (parameter_out.indoorNum > 1)
        {
            if (f_commData12_b4 == 1)
            {
                f_RoomUnitOn = 0;
            }
            else
            {
                if (b_power.run == ON && f_CompOn == 1 && parameter_out.eve_value >= 70)
                {
                    f_RoomUnitOn = 1;
                }
                else
                {
                    f_RoomUnitOn = 0;
                }
            }
        }
        else
        {
            if (f_CompOn == 0)
            {
                f_RoomUnitOn = 0;
            }
            else
            {
                if (b_power.run && f_CompOn)
                {
                    f_RoomUnitOn = 1;
                }
                else
                {
                    f_RoomUnitOn = 0;
                }
            }
        }

        // D14
        parameter_out.ac_current = commData14; // 接收到的外机交流电流 * 10

        // D15
        temp.comp_ipm = commData15 * 10; // (实际室外压缩机温值 + 40) * 10
    }
    else
    {
        // D0
        f_CompOn = f_commData0_b4;
        f_ExpRest = f_commData0_b5;
        f_4way = f_commData0_b6;
        f_HeaterOut = f_commData0_b7;

        local_outModeRxd = parameter_out.run_mode;
        parameter_out.run_mode = (commData0 & 0x0F);

        if (parameter_out.run_mode == RXFORCEDEF)
        {
            set_manual_defrost(OFF);
        }

        if (local_outModeRxd == RXFORECALL)
        {
            if (parameter_out.run_mode != RXFORECALL)
            {
                if (f_FoRecall == 1)
                {
                    f_FoRecall = 0;
                    stat_FoRecall = 2;
                }
            }
        }
        else
        {
            if (parameter_out.run_mode == RXFORECALL)
            {
                if (f_FoRecall == 0)
                {
                    f_FoRecall = 1;
                    stat_FoRecall = 1;
                }
            }
        }

        if (local_outModeRxd == RXTESTRUN)
        {
            if (parameter_out.run_mode != RXTESTRUN)
            {
                TestRunStat = 0;
            }
        }
        else
        {
            if (parameter_out.run_mode == RXTESTRUN)
            {
                TestRunStat = 2;
            }
        }

        if (b_power.run == ON && e_mode.run == MODEHEAT)
        {
            if ((parameter_out.run_mode != RXHEAT) && (parameter_out.run_mode != RXFORCEHEAT) && (parameter_out.run_mode != RXFORCEDEF) && (parameter_out.run_mode != RXHEATTEST))
            {
                f_ModeClash = 1;
            }
            else
            {
                f_ModeClash = 0;
            }
        }

        // D2
        parameter_out.comp_freq = commData2;

        /* D3 */
        parameter_out.ofan_speed = commData3;

        /* D4 D5 */
        // if (parameter_out.indoorNum >= 2 && f_ExpExist == 1)
        //{
        parameter_out.eve_value = commData4 & 0x03;
        parameter_out.eve_value = parameter_out.eve_value << 8;
        parameter_out.eve_value += commData5;
        //}
        // else
        //{
        // parameter_out.eve_value = 200;
        //}

        /* D6 */
        parameter_out.ac_voltage = commData6 * 2; /* 接收到的外机交流电压 */

        /* D7 */
        parameter_out.dc_voltage = commData7 * 4; /* 接收到的直流母线电压 */

        /* D8 */
        parameter_out.ac_current = commData8; /* 接收到的外机交流电流 * 10 */

        /* D9 */
        parameter_out.dc_current = commData9; /* 接收到的压缩机相电流 * 10 */

        /* D10 */
        temp.outdoor = commData10 * 10; /* (实际室外环温值 + 40) * 10 */

        /* D11 */
        temp.out_tube = commData11 * 10; /* (实际室外铜管温值 + 40) * 10 */

        /* D12 */
        temp.out_exha = commData12 * 10; /* (实际室外排气温值 + 40) * 10 */

        /* D13 */
        temp.comp_ipm = commData13 * 10; /* (实际室外压缩机温值 + 40) * 10 */

        /* D15 */
        f_KeepHumToFan = f_commData15_b7;

        if (f_commData15_b2)
        {
            f_Defrosting = 1;
            set_manual_defrost(OFF);
        }
        else
        {
            f_Defrosting = 0;
        }

        if (f_commData15_b3)
        {
            TempA = parameter_out.run_mode;

            if (TempA == RXHEAT || TempA == RXFORCEHEAT || TempA == RXHEATTEST || TempA == RXFORCEDEF)
            {
                f_Defrosting = 1;
                f_CoolOilBack = 0;
                f_HeatOilBack = 1;
            }
            else
            {
                f_CoolOilBack = 1;
                f_HeatOilBack = 0;
            }
        }
        else
        {
            f_CoolOilBack = 0;
            f_HeatOilBack = 0;
        }

        f_wait = f_commData15_b4;
        f_outquiet = f_commData15_b5;
        f_outCondensation = f_commData15_b6;

        if (parameter_out.indoorNum > 1)
        {
            if (f_commData0_b5 == 1)
            {
                f_RoomUnitOn = 0;
            }
            else
            {
                if (b_power.run == ON && f_CompOn == 1 && parameter_out.eve_value >= 70)
                {
                    f_RoomUnitOn = 1;
                }
                else
                {
                    f_RoomUnitOn = 0;
                }
            }
        }
        else
        {
            if (f_CompOn == 0)
            {
                f_RoomUnitOn = 0;
            }
            else
            {
                if (b_power.run && f_CompOn)
                {
                    f_RoomUnitOn = 1;
                }
                else
                {
                    f_RoomUnitOn = 0;
                }
            }
        }
    }
}

void RxdOrder12(void)
{
    OutSafeErr0 = commData0;
    OutSafeErr1 = commData1;
    OutSafeErr2 = commData2;
    OutSafeErr3 = commData3;
    OutSafeErr4 = commData4;
    OutSafeErr5 = commData5;
    OutSafeErr6 = commData6;
    OutSafeErr7 = commData7;
    OutSafeErr8 = commData8;

    speedSetValueRxd = commData13;
    temp.gas_pipe = commData14 * 10;
    temp.liquid_pipe = commData15 * 10;
}

void TxdData(void)
{
    if ((ErrFlag && f_FoRecall == 0) || ErrFlagQT || f_eepRomOnOff || f_SwToFanAct == 0 || f_comm485Err)
    {
        f_RoomUnitOff = 1;
    }
    else
    {
        f_RoomUnitOff = 0;
    }

    CommBuffClr();

    switch (txdOrder)
    {
    case ORDER81:
        TxdOrder81();
        break;

    case ORDER82:
        TxdOrder82();
        break;

    case ORDER8F:
        TxdOrder8F();
        break;

    case ORDER91:
        TxdOrder91();

        timer_CommInOut.commRstErr = 0;
        break;

    case ORDER92:
        TxdOrder92();
        break;
    }
}

void TxdOrder81(void)
{
    commOrder = 0x81;

    commAddr = slaveAddr | 0x20;

    if (f_skflg == 1)
    {
        if (f_TxdResetReq == 1)
        {
            f_commFlag_b2 = 1;
        }

        /* D0 */ /*CW*/
        commData0 = 0x07;

        /* D1 */
        if (f_coldOnly)
        {
            commData1 = 0x22;
        }
        else
        {
            commData1 = 0x20;
        }

        /* D2 */
        commData2 = sysAbility;

        /* D3 */
        commData3 = 0x02;
        f_commData3_b3 = ~f_inPower;

        // D4
        commData4 = 0x00; // Free

        // D5
#ifdef MACHINE_TYPE_BG
        commData5 = 0x10; // 壁挂机
#endif                    // MACHINE_TYPE_BG

#ifdef MACHINE_TYPE_ZD
        commData5 = 0x30; // 座吊机
#endif                    // MACHINE_TYPE_ZD

#ifdef MACHINE_TYPE_FG
        commData5 = 0x40; // 风管机
#endif                    // MACHINE_TYPE_FG

#ifdef MACHINE_TYPE_TJ
        commData5 = 0x50; // 天井机
#endif                    // MACHINE_TYPE_TJ

        // D7
        commData7 = 0x14;

        // D8
#ifdef PG_MOTOR
        commData8 = 0x13;
#else
        commData8 = 0x23;
#endif

        // D9
        commData9 = 0x7A;
        f_commData9_b4 = f_eepRom;

        // D11
        commData11 = jumpType;
    } /*CW end*/
    else
    {
        if (f_TxdResetReq == 1)
        {
            f_commFlag_b2 = 1;
        }

        // D0
        commData0 = 0x01;

        // D1
        if (f_coldOnly)
        {
            commData1 = 0x02;
        }
        else
        {
            commData1 = 0x00;
        }

        // D2
        commData2 = sysAbility;

        // D3
        commData3 = 0x02;
        f_commData3_b3 = ~f_inPower;

        // D4
#ifdef MACHINE_TYPE_BG
        commData4 = 0x00; // Free
#endif                    // MACHINE_TYPE_BG

#ifdef MACHINE_TYPE_ZD
        commData4 = 0x06; // 亚洲
#endif                    // MACHINE_TYPE_ZD

#ifdef MACHINE_TYPE_FG
        commData4 = 0x00; // Free
#endif                    // MACHINE_TYPE_FG

#ifdef MACHINE_TYPE_TJ
        commData4 = 0x04; // 欧洲
#endif                    // MACHINE_TYPE_TJ

        /* D5 */
#ifdef MACHINE_TYPE_BG
        commData5 = 0x01; // 壁挂机
#endif                    // MACHINE_TYPE_BG

#ifdef MACHINE_TYPE_ZD
        commData5 = 0x03; // 座吊机
#endif                    // MACHINE_TYPE_ZD

#ifdef MACHINE_TYPE_FG
        commData5 = 0x04; // 风管机
#endif                    // MACHINE_TYPE_FG

#ifdef MACHINE_TYPE_TJ
        commData5 = 0x05; // 天井机
#endif                    // MACHINE_TYPE_TJ

        /* D6 */
        commData6 = jumpType;

        /* D7 */
        commData7 = 0x03;

        /* D8 */
#ifdef PG_MOTOR
        commData8 = 0x19;
#else
        commData8 = 0x1A;
#endif

        /* D9 */
        commData9 = 0x7A;
        f_commData9_b4 = f_eepRom;
    }
    if (f_skflg == 0)
    {
        commCheck = CommGetBCC();
    }
    else
    {
        commCheck = CommGetBCC_SK();
}
}


void TxdOrder82(void)
{
    commOrder = 0x82;

    commAddr = slaveAddr | 0x20;
    if (f_skflg == 1)
    { /*CW*/
        if (f_TxdResetReq == 1)
        {
            f_commFlag_b2 = 1;
        }
    }
    else
    {
        if (f_TxdResetReq == 1)
        {
            f_commFlag_b2 = 1;
        }

        /* D0 ~ D4 */
        if ( Fanmotor_Type == OFF ) // 直流风机
        {
            commData0 = BOARD_CODING_DC_FAN_0;
            commData1 = BOARD_CODING_DC_FAN_1;
            commData2 = BOARD_CODING_DC_FAN_2;
            commData3 = BOARD_CODING_DC_FAN_3;
            commData4 = BOARD_CODING_DC_FAN_4;
        }
        else // 交流风机
        {
            commData0 = BOARD_CODING_AC_FAN_0;
            commData1 = BOARD_CODING_AC_FAN_1;
            commData2 = BOARD_CODING_AC_FAN_2;
            commData3 = BOARD_CODING_AC_FAN_3;
            commData4 = BOARD_CODING_AC_FAN_4;
        }

        /* D5 ~ D8 */
        commData5 = PROGRAM_CODING_0;
        commData6 = PROGRAM_CODING_1;
        commData7 = PROGRAM_CODING_2;
        commData8 = PROGRAM_CODING_3;

        /* D9 */
        commData9 = PROGRAM_VER;
    }
    /* D10 ~ D15 */

    /* D10 ~ D15 */
    if (f_skflg == 0)
    {
        commCheck = CommGetBCC();
    }
    else
    {
        commCheck = CommGetBCC_SK();
}
}


void TxdOrder8F(void)
{
    commOrder = 0x8F;

    commAddr = slaveAddr | 0x20;

    if (f_TxdResetReq == 1)
    {
        f_commFlag_b2 = 1;
    }

    if (f_skflg == 0)
    {
        commCheck = CommGetBCC();
    }
    else
    {
        commCheck = CommGetBCC_SK();
}
}


void TxdOrder91(void)
{
    U8 TempA;

    commOrder = 0x91;

    commAddr = slaveAddr | 0x20;

    if (f_skflg == 1)
    {
        if (f_TxdResetReq == 1)
        {
            f_commFlag_b2 = 1;
        }

        // D0
        if (b_manual_defrost.set == 1)
        {
            commData0 = 0x07;
        }
        else
        {
            if (f_FoRecall == 1)
            {
                commData0 = 0x0A;
            }
            else
            {
                if (TestRunStat)
                {
                    commData0 = 0x0B;
                }
                else
                {
                    if ((CapTestStat || SetFreValue))
                    {
                        if (e_mode.run == MODECOOL)
                        {
                            commData0 = 0x08;
                        }
                        else
                        {
                            commData0 = 0x09;
                        }
                    }
                    else
                    {
                        commData0 = e_mode.run;
                    }
                }
            }
        }

        if (b_power.run == ON)
        {
            f_commData0_b4 = 1;
        }

        if (l_timerOff.set != 0)
        {
            f_commData0_b5 = 1;
        }

        if (l_timerOn.set != 0)
        {
            f_commData0_b6 = 1;
        }

        if (f_RoomUnitOff == 1)
        {
            f_commData0_b7 = 1;
        }

        // D1
        if (e_fanStep.run != FANSTOP)
        {
            if (CapTestStat != 0 || (SetFreValue != 0 && f_TestFanChg == 0))
            {
                commData1 = 0x07;
            }
            else if (e_fanStep.run < 5)
            {
                commData1 = 0x01;
            }
            else if (e_fanStep.run < 20)
            {
                commData1 = 0x02;
            }
            else if (e_fanStep.run < 40)
            {
                commData1 = 0x03;
            }
            else if (e_fanStep.run < 60)
            {
                commData1 = 0x04;
            }
            else if (e_fanStep.run < 80)
            {
                commData1 = 0x05;
            }
            else if (e_fanStep.run < 95)
            {
                commData1 = 0x06;
            }
            else
            {
                commData1 = 0x07;
            }
        }

        // D2
        if ((temp.room % 10) >= 5)
        {
            commData2 = 0x50;
        }

        if ((temp.tube % 10) >= 5)
        {
            f_commData2_b3 = 1;
        }

        if ((l_tarTemp_C.run % 10) == 5)
        {
            f_commData2_b1 = 1;
        }

        // D3
        if (CapTestStat || SetFreValue)
        {
            if (e_mode.run == MODECOOL || e_mode.run == MODEDRY)
            {
                TempA = 16;
            }
            else
            {
                TempA = 31;
            }
        }
        else
        {
            TempA = l_tarTemp_C.run / 10;
        }

        commData3 = TempA + 40;

        // D4
        commData4 = temp.room / 10;

        // D5
        commData5 = speedNowValue1 / 10;

        // D6
        commData6 = temp.tube / 10;

        // D7
        if (e_mode.run == MODEHEAT)
        {
            commData7 = ((temp.repair) << 1);
            f_commData7_b0 = 1;
        }

        f_commData7_b5 = f_CleanSelf;

        // D9
        f_commData9_b0 = f_quickTest;
        f_commData9_b1 = f_Heater;
        f_commData9_b2 = f_antidewing;

        if (stat_AvoidCold <= ACLOW && f_RoomUnitOn == 1 && e_mode.run == MODEHEAT)
        {
            f_commData9_b3 = 1;
        }

        f_commData9_b6 = b_eco.run;

        // D10
        commData10 |= CapTestStat;

        f_commData10_b5 = f_SetNoCheckFo;

        // D11
        commData11 = SetFreValue;

        // D12 D13
        commData12 = (U8)((SetExpValue & 0xFF00) >> 8);
        commData13 = (U8)(SetExpValue & 0x00FF);

        if (SetExhValue != 0)
        {
            f_commData12_b5 = 1;
        }

        if (SetOutFanValue != 0)
        {
            f_commData12_b6 = 1;
        }

        if (SetExpValue != 0)
        {
            f_commData12_b7 = 1;
        }

        // D14
        commData14 = SetOutFanValue / 10;

        // D15
        commData15 = SetExhValue;
    }
    else
    {
        if (f_TxdResetReq == 1)
        {
            f_commFlag_b2 = 1;
        }

        // D0
        if (b_manual_defrost.set == 1)
        {
            commData0 = 0x07;
        }
        else
        {
            if (f_FoRecall == 1)
            {
                commData0 = 0x0A;
            }
            else
            {
                if (TestRunStat)
                {
                    commData0 = 0x0B;
                }
                else
                {
                    if ((CapTestStat || SetFreValue))
                    {
                        if (e_mode.run == MODECOOL)
                        {
                            commData0 = 0x08;
                        }
                        else
                        {
                            commData0 = 0x09;
                        }
                    }
                    else
                    {
                        commData0 = e_mode.run;
                    }
                }
            }
        }

        if (b_power.run == ON)
        {
            f_commData0_b4 = 1;
        }

        if (f_RoomUnitOff == 1)
        {
            f_commData0_b7 = 1;
        }

        /* D1 */
        if (e_fanStep.run != FANSTOP)
        {
            if (CapTestStat != 0 || (SetFreValue != 0 && f_TestFanChg == 0))
            {
                commData1 = 0x07;
            }
            else if (e_fanStep.run < 5)
            {
                commData1 = 0x01;
            }
            else if (e_fanStep.run < 20)
            {
                commData1 = 0x02;
            }
            else if (e_fanStep.run < 40)
            {
                commData1 = 0x03;
            }
            else if (e_fanStep.run < 60)
            {
                commData1 = 0x04;
            }
            else if (e_fanStep.run < 80)
            {
                commData1 = 0x05;
            }
            else if (e_fanStep.run < 95)
            {
                commData1 = 0x06;
            }
            else
            {
                commData1 = 0x07;
            }
        }

        commData1 |= (CapTestStat << 4);

        /* D2 */
        if (CapTestStat || SetFreValue)
        {
            if (e_mode.run == MODECOOL || e_mode.run == MODEDRY)
            {
                TempA = 16;
            }
            else
            {
                TempA = 31;
            }
        }
        else
        {
            TempA = l_tarTemp_C.run / 10;
        }

        commData2 = TempA + 40;

        /* D3 */
        commData3 = temp.room / 10;

        /* D4 */
        commData4 = temp.tube / 10;

        /* D5 */
        if ((temp.room % 10) >= 5)
        {
            commData5 = 0x50;
        }

        if ((temp.tube % 10) >= 5)
        {
            f_commData5_b3 = 1;
        }

        if ((l_tarTemp_C.run % 10) == 5)
        {
            f_commData5_b1 = 1;
        }

        /* D6 */
        commData6 = l_tarHum.run / 10;

        /* D7 */
        commData7 = htu20.hum / 10;

        /* D8 */
        f_commData8_b0 = f_quickTest;
        f_commData8_b1 = f_Heater;
        f_commData8_b2 = f_antidewing;

        if (stat_AvoidCold <= ACLOW && f_RoomUnitOn == 1 && e_mode.run == MODEHEAT)
        {
            f_commData8_b3 = 1;
        }

        f_commData8_b4 = f_SetNoCheckFo;

        f_commData8_b6 = b_eco.run;

        /* D9 */
        if (l_timerOn.set != 0)
        {
            f_commData9_b7 = 1;
        }

        if (l_timerOff.set != 0)
        {
            f_commData9_b6 = 1;
        }

        f_commData9_b5 = f_CleanSelf;
        f_commData9_b4 = f_SetKeepHum;

        /* D10 */
        if (e_mode.run == MODEHEAT)
        {
            commData10 = (temp.repair << 4);
            f_commData10_b3 = 1;
        }

        if (SetExhValue != 0)
        {
            f_commData10_b0 = 1;
        }

        if (SetExpValue != 0)
        {
            f_commData10_b1 = 1;
        }

        if (SetOutFanValue != 0)
        {
            f_commData10_b2 = 1;
        }

        /* D11 */
        commData11 = SetFreValue;

        /* D12 */
        commData12 = SetOutFanValue / 10;

        /* D13 D14 */
        commData13 = (U8)((SetExpValue & 0xFF00) >> 8);
        commData14 = (U8)(SetExpValue & 0x00FF);

        /* D15 */
        commData15 = SetExhValue;
    }

    if (f_skflg == 0)
    {
        commCheck = CommGetBCC();
    }
    else
    {
        commCheck = CommGetBCC_SK();
}
}


void TxdOrder92(void)
{
    U16 tempi;

    commOrder = 0x92;

    commAddr = slaveAddr | 0x20;

    if (f_skflg == 1)
    {
        if (f_TxdResetReq == 1)
        {
            f_commFlag_b2 = 1;
        }

        /* D0 */
        if (f_RoomUnitOff == 1)
        {
            f_commData0_b7 = 1;
        }

        f_commData0_b0 = f_RoomErr;
        f_commData0_b1 = f_TubeErr;
        f_commData0_b2 = f_GasLeakErr;

        /* D1 */
        // f_commData1_b6 = f_KeyLock;
        f_commData1_b5 = f_HeaterErr;
        // f_commData1_b4 = f_eepErr;
        f_commData1_b3 = f_selErr;
        f_commData1_b2 = f_MotorErr;
        f_commData1_b0 = f_JumpErrQT;

        // D2 D3
        commData2 = 0x00;
        commData3 = 0x00;

        // D5
        commData5 = speedSetValue / 10;

        // D6
        tempi = ver_Step.SwNow;
        tempi = tempi * 45;
        tempi = tempi / 64;
        commData6 = (U8)(tempi);

        // D8 D9
        if (speedNowValue1 <= 2500)
        {
            tempi = speedNowValue1;
            commData8 = (U8)((tempi & 0xFF00) >> 8);
            commData9 = (U8)(tempi & 0x00FF);
        }
        else if (speedNowValue1 != 0x0)
        {
            commData8 = 0x0A;
            commData9 = 0x28;
        }
        else
        {
            commData8 = 0;
            commData9 = 0;
        }

        // D12
        tempi = hor_Step.SwNow;
        tempi = tempi * 45;
        tempi = tempi / 64;
        // commData12 = (U8)(tempi);
        // commData12 = hidi_env / 10; //(device_sensor.H_env + environment.humidity + htu20.hum) / 10;;

        // D9 D10
        // commData9 = ;
        // commData10 = ;
    }
    else
    {
        if (f_TxdResetReq == 1)
        {
            f_commFlag_b2 = 1;
        }

        // D0
        if (f_RoomUnitOff == 1)
        {
            f_commData0_b7 = 1;
        }

        f_commData0_b0 = f_RoomErr;
        f_commData0_b1 = f_TubeErr;
        f_commData0_b2 = f_GasLeakErr;

        // D1
        // f_commData1_b6 = f_KeyLock;
        f_commData1_b5 = f_HeaterErr;
        // f_commData1_b4 = f_eepErr;
        f_commData1_b3 = f_selErr;
        f_commData1_b2 = f_MotorErr;
        f_commData1_b0 = f_JumpErrQT;

        // D2
        commData2 = 0x00;

        /* D3 D4 */
        if (speedSetValue != 0)
        {
            tempi = speedSetValue;
            commData3 = (U8)((tempi & 0xFF00) >> 8);
            commData4 = (U8)(tempi & 0x00FF);
        }
        else
        {
            commData3 = 0;
            commData4 = 0;
        }

        /* D5 D6 */
        if (speedNowValue1 <= 2500)
        {
            tempi = speedNowValue1;
            commData5 = (U8)((tempi & 0xFF00) >> 8);
            commData6 = (U8)(tempi & 0x00FF);
        }
        else if (speedNowValue1 != 0x0)
        {
            commData5 = 0x0A;
            commData6 = 0x28;
        }
        else
        {
            commData5 = 0;
            commData6 = 0;
        }

        /* D7 */
        tempi = ver_Step.SwNow;
        tempi = tempi * 45;
        tempi = tempi / 64;
        commData7 = (U8)(tempi);

        /* D8 */
        tempi = hor_Step.SwNow;
        tempi = tempi * 45;
        tempi = tempi / 64;
        commData8 = (U8)(tempi);

        /* D9 D10 */
        // commData9 = ;
        // commData10 = ;
    }

    if (f_skflg == 0)
    {
        commCheck = CommGetBCC();
    }
    else
    {
        commCheck = CommGetBCC_SK();
}
}


void CommInOut_timer_500us(void) /* called in systime.c per 500us */
{
    if (timer_CommInOut.tsreset == 0)
    {
        timer_CommInOut.commRun++;
    }

    if (timer_CommInOut.txEndDelay > 0)
    {
        timer_CommInOut.txEndDelay--;
    }

    if (timer_CommInOut.tsfrmwt > 0)
    {
        timer_CommInOut.tsfrmwt--;
    }
}

void CommInOut_time_1s(void) /* called in systime.c per 1s */
{
    if (e_mode.run != MODEFAN && b_power.run)
    {
        CommErrCheck();
    }
    else
    {
        timer_CommInOut.commErr = 0;
    }

    if (timer_CommInOut.commRstErr > 0)
    {
        timer_CommInOut.commRstErr--;

        if (timer_CommInOut.commRstErr == 0)
        {
#ifndef KONGKE
            f_CommErr = 1;
            f_RoomUnitOn = 0;
#endif

            f_TxdResetReq = 1;
        }
    }

    if (f_eepRomOnOff)
    {
        if (CompOffSec >= (180 + compOnRandomRun))
        {
            f_eepRomOnOff = 0;
        }
    }

    if (timer_CommInOut.noComm < 18)
    {
        timer_CommInOut.noComm++;
    }
}

void CommErrCheck(void)
{
    if (f_CommErr == 0)
    {
        if (timer_CommInOut.commErr < T_SRXERR) // 1s * 180 = 180s
        {
            timer_CommInOut.commErr++;
        }
        else
        {
            timer_CommInOut.commErr = 0;

#ifndef KONGKE
            f_CommErr = 1;
            f_RoomUnitOn = 0;
            f_CommToOff = 1;
#endif

            f_TxdResetReq = 1;
        }
    }
}
/* End user code. Do not edit comment generated here */
